package com.example.demo.controller;

import com.example.demo.model.Active;
import com.example.demo.model.Comment;
import com.example.demo.model.LoginUser;
import com.example.demo.model.TokenUser;
import com.example.demo.service.AdminService;
import com.example.demo.service.EmployeeService;
import com.example.demo.tools.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WJ
 * Date: 2023-02-17
 * Time: 21:35
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private EmployeeService employeeService;

//    管理员权限时，首页的table数据
    @RequestMapping("/gettablelist")
    public Object getPersonTable(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();
        return adminService.getPersonTable(user);
    }

//    查询所有的管理员信息（要么根据id查一条，要么查除当前登录id之外的全部）
    @RequestMapping("/getalladmininfo")
    public Object getAllAdminInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();
        return adminService.getAllAdminInfo(user);
    }

//    根据 adminId 查询下属员工的所有信息(管理员管理功能)
    @RequestMapping("/getallemployeebyadminid")
    public Object getAllEmployeeByAdminId(@RequestParam Integer adminId){
        if(adminId != null){
            return adminService.getAllEmployeeByAdminId(adminId);
        }
        return "请传入正确的管理员id";
    }

//    获取当前登录用户下所管理的全部员工信息
    @RequestMapping("/getallemployee")
    public Object getAllEmployee(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();
        return adminService.getAllEmployee(user);
    }

//    重置员工的分数
    @RequestMapping("/updateemployeegrade")
    public Object updateEmployeeGrade(@RequestParam Integer id){

        return adminService.updateEmployeeGrade(id);
    }

//    更新员工信息
    @RequestMapping("/updateemployeeinfo")
    public Object updateEmployeeInfo(@RequestParam Integer id,
                                     String username,
                                     Integer age,
                                     String phone,
                                     String area,
                                     String address,
                                     String appraise,
                                     @RequestParam(value = "admin_id" ,required = false) Integer adminId
    ){
        return adminService.updateEmployeeInfo(id,username,age,phone,area,adminId,address,appraise);
    }

//    删除员工
    @RequestMapping("/deleteemployeeforadmin")
    public Object deleteEmployeeForAdmin(@RequestParam Integer id){
        return adminService.deleteEmployeeForAdmin(id);
    }


//    直接查全部管理员信息(只有 id 和 username 字段)
    @RequestMapping("/getalladmin")
    public Object getAllAdmin(){
        return adminService.getAllAdmin();
    }

//    获取当前管理员下的所有活动信息
    @RequestMapping("/getactiveinfobyadmin")
    public Object getActiveInfoByAdmin(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();
        return adminService.getActiveInfoByAdmin(user.getId());
    }

//    活动通过
    @RequestMapping("/activeby")
    public Object activeBy(@RequestParam Integer id){
        return adminService.activeBy(id);
    }

//    活动删除(不通过)
    @RequestMapping("/activedelete")
    public Object activeDelete(@RequestParam Integer id){
        return adminService.activeDelete(id);
    }

//    获取所有的活动信息
    @RequestMapping("/getallactiveinfo")
    public Object getAllActiveInfo(){
        return adminService.getAllActiveInfo();
    }

    //    活动的修改
    @RequestMapping("/updatedetailactiveinfo")
    public Object updateDetailActiveInfo(Active active){
        if(active == null){
            return "参数不能为空!";
        }
        return adminService.updateDetailActiveInfo(active);
    }

//    通过活动id获取所有的评论信息
    @RequestMapping("/getallcommentinfobyid")
    public Object getAllCommentByActiveId(@RequestParam Integer id){
        return adminService.getAllCommentByActiveId(id);
    }

//    添加评论
    @RequestMapping("/addcomment")
    @CrossOrigin
    public Object addComment(@RequestParam("active_id") Integer activeId,
                             @RequestParam("context") String context){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();

        Comment comment = new Comment();
        comment.setActive_id(activeId);
        comment.setComment_id(user.getId());
        comment.setContext(context);

        return adminService.addComment(comment);
    }



//    修改管理员/员工的信息
    @RequestMapping("/updateadmininfo")
    public Object updateAdminInfo(@RequestParam Integer id,
                                  @RequestParam String username,
                                  String password,
                                  @RequestParam String sex,
                                  @RequestParam Integer age,
                                  @RequestParam String phone,
                                  @RequestParam String url,
                                  @RequestParam String address,
                                  @RequestParam String status){

        if("员工".equals(status)){
            return employeeService.updateAdminInfo(id,username,password,sex,age,phone,url,address);
        }else{
            return adminService.updateAdminInfo(id,username,password,sex,age,phone,url,address);
        }
    }

//    根据条件查询（管理员模块的查询）
    @RequestMapping("/getallbylike")
    public Object getAllAdminInfoByUsernameOrAgeD(String username,
                                                  String phone,
                                                  String sex,
                                                  Integer age){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();
        return adminService.getAllAdminInfoByUsernameOrAgeD(user.getId(),username,phone,sex,age);
    }

//    获取老人用户给员工评价的信息
    @RequestMapping("/getemployeeoldercomment")
    public Object getEmployeeOlderComment(){

        return adminService.getEmployeeOlderComment();
    }
}
